<?php
include "templates/header.php";

if ($_POST) {
    if (isset($_POST['add_new'])) {
        $add_fields = array();
        unset($_POST['add_new']);
        foreach ($_POST as $key => $value) {
            $add_fields[$key] = addslashes($value);
        }
        if(!$add_fields['active']){
            $add_fields['active'] = 0;
        }
        mysql_insert('category', $add_fields);
    }
    if (isset($_POST['update_category'])) {
        $update_fields = array();
        foreach ($_POST['data'][$_POST['update_category']] as $key => $value) {
            $update_fields[$key] = addslashes($value);
        }
        if(!$update_fields['active']){
            $update_fields['active'] = 0;
        }
        mysql_update('category', $update_fields, $_POST['update_category']);
    }
    if (isset($_POST['delete_category'])) {
        mysql_delete('category', $_POST['delete_category']);
    }
}

$categories = mysql_select_array("SELECT * FROM `category`");
?>
<table border="1" cellspacing="0" style="width: 100%; text-align: center;">
    <tr>
        <td>Название</td>
        <td style="width: 80px;" title="от 0 до 255. 0 - максимальный приоритет">Приоритет</td>
        <td style="width: 80px;">Показать</td>
    </tr>
    <form method="post" action="">
        <input type="hidden" name="add_new" value="1" />
        <tr>
            <td><input type="text" style="width: 100%;" name="title" /></td>
            <td><input type="text" style="width: 100%;" name="priority" /></td>
            <td><input type="checkbox" name="active" value="1" checked="checked" /></td>
        </tr>
        <tr>
            <td colspan="2">
                <textarea name="description" style="width: 100%; height: 150px;">&nbsp;</textarea>
            </td>
            <td>
                <input type="submit" value="Добавить">
            </td>
        </tr>
    </form>
    <?foreach($categories as $key=>$category):?>
        <form method="post" action="">
            <input type="hidden" name="update_category" value="<?=$category['id'];?>" />
            <tr>
                <td><input type="text" style="width: 100%;" name="data[<?=$category['id'];?>][title]" value="<?=stripslashes($category['title']);?>" /></td>
                <td><input type="text" style="width: 100%;" name="data[<?=$category['id'];?>][priority]" value="<?=$category['priority'];?>" /></td>
                <td><input type="checkbox" style="width: 100%;" name="data[<?=$category['id'];?>][active]" value="1" <?if($category['active']):?>checked="checked"<?endif;?> /></td>
            </tr>
            <tr>
                <td colspan="2">
                    <textarea style="width: 100%; height: 150px;" name="data[<?=$category['id'];?>][description]"><?=stripslashes($category['description']);?></textarea>
                </td>
                <td>
                    <input type="submit" value="Обновить"></form><br />
                    <form action="" method="post"><input type="hidden" name="delete_category" value="<?=$category['id'];?>"><input type="submit" value="Удалить" /></form>
                </td>
            </tr>
        
    <?  endforeach;?>
</table>

<? include "templates/footer.php"; ?>